import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    id:'',
    isBack:false,
    commit_name_id:'',
    commit_name:'',
    ico:'',
    hallId:'',
    time_popup_show:false,
    type_popup_show:false,
    typeOption:[],
    recordDate:'',
    currentTimestamp:'',
    mealType_selected:'',
    hallName:'',
    staffOption:[],
    staffNum:0,
  },
  chooseStaff() {
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
  
  /**
   * 选择餐别
   */
  choosePayType(e){
    let type = e.currentTarget.dataset.type;
    let that = this;
    api.curlPost('?cla=canteenApp&fun=replaceReportSearch').then((res)=>{
        if(type == 'hall'){
          that.setData({
            nowType:type,
            type_popup_show:true,
            typeOption:res.data.hallOption
          })
        }else if(type =='mealType'){
          that.setData({
            nowType:type,
            type_popup_show:true,
            typeOption:res.data.mealOption
          })
        }
      })
  },

  /**餐别确认 */
  onTypeConfirm(event){
    const { picker, value, index } = event.detail;
    if(this.data.nowType =='hall'){
      console.log(value);
      this.setData({
        hallName:value.name,
        hallId:value.id,
        type_popup_show:false
      })
    
    }else if(this.data.nowType == 'mealType'){
      this.setData({
        mealType_selected:value,
        type_popup_show:false
      })
    }
  },
  onTypeCancel(e){
    this.setData({
      type_popup_show:false
    })
  },
  commit(e){
    let para = e.detail.value;
    para.hallId = this.data.hallId;
    para.staffOption = JSON.stringify(this.data.staffOption);
    para.type='代取餐';
    wx.showModal({
      title: '提示',
      content: '是否确认取餐？',
      success (res) {
        if (res.confirm) {
          api.curlPost('?cla=canteenApp&fun=takeMealEdit',para).then((res)=>{
            Toast(res.warn);
             wx.navigateBack({
               delta: 1,
             })
          })
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let currentTimestamp = new Date().getTime();
    let date = new Date(currentTimestamp);
    this.setData({
      recordDate: api.formatDateCRM('Y-m-d',date),
      currentTimestamp:currentTimestamp
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  onSwipeClose(event){
    let that = this;
    const { position, instance } = event.detail;
    switch (position) {
      case 'right':
        that.deleteData(event.currentTarget.dataset.id)
        break;
    }
  },
  
  /**删除数据 */
  deleteData(id){
    let list = this.data.staffOption;
    let newList = list.filter(item => item.stid !=id );
    this.setData({
      staffOption:newList
    })
    this.countStaff();
  },

  /**计算人数 */
  countStaff(){
    this.setData({
      staffNum:this.data.staffOption.length
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.isBack){
      this.setData({
        isBack:false,
      });
      let flag = true;
      that.data.staffOption.forEach((v)=>{
        if(v.stid == that.data.commit_name_id){
           flag = false;//已存在此员工
        }
      })
      if(flag){
        api.curlPost('?cla=auditApp&fun=getStaffInfo',{staffId:this.data.commit_name_id}).then((res)=>{
          let arr = that.data.staffOption.concat(res.data.result);
          that.setData({
            staffOption:arr
          })
          that.countStaff();
        })
      }
    }
  },
/**选择时间 */
chooseTime(e){
  let currentTimestamp = this.data.recordDate?new Date(api.rowTocol(this.data.recordDate)).getTime():new Date().getTime();
  this.setData({
    time_popup_show:true,
    currentTimestamp:currentTimestamp
  })

},
/**日期选择确认 */
timeConfirm(event){
  let date = new Date(event.detail);
    this.setData({
      recordDate: api.formatDateCRM('Y-m-d',date),
      time_popup_show:false,
    })
},
timeCancel(e){
  this.setData({
    time_popup_show:false
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})